package com.wowoai.wallpaper.service.impl;

import com.github.wenhao.jpa.PredicateBuilder;
import com.github.wenhao.jpa.Sorts;
import com.github.wenhao.jpa.Specifications;
import com.wowoai.common.factoy.OssClientFactory;
import com.wowoai.common.userInfo.UserInfo;
import com.wowoai.common.page.Pagination;
import com.wowoai.utils.IdGeneratorUtil;
import com.wowoai.wallpaper.convert.WallpaperConvert;
import com.wowoai.wallpaper.entity.WallpaperEntity;
import com.wowoai.wallpaper.repository.WallpaperRepository;
import com.wowoai.wallpaper.service.WallpaperServiceI;
import com.wowoai.wallpaper.service.dto.WallpaperDTO;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class WallpaperServiceImpl implements WallpaperServiceI {

    private final WallpaperRepository wallpaperRepository;

    private final OssClientFactory ossClientFactory;

    public WallpaperServiceImpl(WallpaperRepository wallpaperRepository, OssClientFactory ossClientFactory) {
        Assert.notNull(wallpaperRepository,"wallpaperRepository 不能为空");
        Assert.notNull(ossClientFactory,"ossClientFactory 不能为空");
        this.wallpaperRepository = wallpaperRepository;
        this.ossClientFactory = ossClientFactory;
    }


    @Override
    public Pagination<WallpaperDTO> getWallpaperList(Integer pageNum, Integer pageSize) {
        PredicateBuilder<WallpaperEntity> builder = Specifications.and();
        builder.eq("deleted", false);
        Sort sort = Sorts.builder().desc("createTime").build();
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize,sort);
        Page<WallpaperEntity> wallpaperEntityPage = wallpaperRepository.findAll(builder.build(), pageable);
        List<WallpaperEntity> wallpaperEntityList = wallpaperEntityPage.getContent();
        List<WallpaperDTO> wallpaperDTOS = WallpaperConvert.INS.entity2DTO(wallpaperEntityList);
        return new Pagination<>(wallpaperEntityPage.getPageable().getPageNumber() + 1, wallpaperEntityPage.getPageable().getPageSize(),
                (int) wallpaperEntityPage.getTotalElements(), wallpaperDTOS);
    }

    @Override
    public void uploadFile(UserInfo userInfo, MultipartFile file) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
        String filePath = simpleDateFormat.format(new Date());
        String name = "wallpaper" + System.currentTimeMillis();
        String url = ossClientFactory.upload(file, filePath, null);
        if (url != null) {
            WallpaperEntity wallpaperEntity = WallpaperEntity.create(IdGeneratorUtil.nextId(),userInfo.getUserId(), userInfo.getUserName());
            wallpaperEntity.setName(name);
            wallpaperEntity.setUrl(url);
            wallpaperRepository.save(wallpaperEntity);
        }
    }
}
